package com.ddwl.wallet.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.base.BaseBo;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.wallet.CouponBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.wallet.CouponVo;
import com.ddwl.wallet.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @Author hemiaojun
 * @Date 2021/9/7 16:25
 */
@Api(tags = "优惠卷")
@RequestMapping(value = "/coupon")
@RestController
@RequiredArgsConstructor
public class CouponController extends BaseController {

    private final CouponService couponService;

    @PostMapping("/{id}/info")
    @ApiOperation("券详情")
    @Auth(required = false)
    public RestVo<CouponVo> info(@PathVariable String id) {
        return RestVo.ok(couponService.info(id));
    }

    @PostMapping("/create")
    @ApiOperation("新增券")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.CONSOLE})
    public RestVo create(@Validated(value = BaseBo.Create.class) @RequestBody CouponBo bo) {
        fillBaseField(bo);
        couponService.create(bo);
        return RestVo.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改券")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.CONSOLE})
    public RestVo update(@Validated(value = BaseBo.Update.class) @RequestBody CouponBo bo) {
        fillBaseField(bo);
        couponService.update(bo);
        return RestVo.ok();
    }

    @PostMapping("/page")
    @ApiOperation("分页查询券")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.CONSOLE})
    public RestVo<PageVo<CouponVo>> page(@Validated @RequestBody PageBo<CouponBo> pageBo) {
        CouponBo param = Optional.ofNullable(pageBo.getParam()).orElse(new CouponBo());
        if (!getProduct().equals(BaseEnum.ProductEnum.CONSOLE.getName())) {
            param.setShopId(getShopId());
        }
        pageBo.setParam(param);
        return RestVo.ok(couponService.page(pageBo));
    }

    @GetMapping("/shop")
    @ApiOperation("店铺当前有效券")
    @Auth(required = false, product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE})
    public RestVo<List<CouponVo>> shop(@RequestParam String shopId) {
        return RestVo.ok(couponService.shop(shopId));
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除券")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.CONSOLE})
    public RestVo delete(@Validated(value = BaseBo.Update.class) @RequestBody CouponBo bo) {
        fillBaseField(bo);
        couponService.delete(bo);
        return RestVo.ok();
    }

}
